<template>
  <div>
    <!-- 新增账号按钮 -->
    <el-button type="primary" @click="handleAddData">新增账号</el-button>
    
    <!-- 账号列表 -->
    <div class="account-management">
      <el-card>
        <template #header>
          <div class="clearfix">
            <span>账号列表</span>
          </div>
        </template>
        <el-table :data="accounts" style="width: 100%">
          <el-table-column prop="id" label="ID"></el-table-column>
          <el-table-column prop="username" label="用户名"></el-table-column>
          <el-table-column prop="password" label="密码"></el-table-column>
          <el-table-column fixed="right" label="操作">
            <template #default="scope">
              <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    
    <!-- 新增账号模态框 -->
    <el-dialog
      title="新增账号"
      v-model="addDataDialogVisible"
      @close="resetAddForm"
    >
      <el-form :model="addFormData" status-icon>
        <el-form-item label="用户名" prop="username" :rules="[{ required: true, message: '请输入用户名', trigger: 'blur'}]">
          <el-input v-model="addFormData.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="addFormData.password"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDataDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleAdd">添加</el-button>
        </span>
      </template>
    </el-dialog>
    
    <!-- 编辑账号模态框 -->
    <el-dialog
      title="编辑账号"
      v-model="editDataDialogVisible"
      @close="resetEditForm"
    >
      <el-form :model="editFormData" status-icon>
        <el-form-item label="用户名" prop="username" :rules="[{ required: true, message: '请输入用户名', trigger: 'blur'}]">
          <el-input v-model="editFormData.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="editFormData.password"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDataDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import axios from 'axios';
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();
const accounts = ref([]);
const addDataDialogVisible = ref(false);
const editDataDialogVisible = ref(false);
const addFormData = ref({
  username: '',
  password: ''
});
const editFormData = ref({});

onMounted(() => {
  getAdmin();
});

function getAdmin() {
  axios.get("http://localhost:8080/user/getAdmin").then((res) => {
    accounts.value = res.data;
  }).catch((error) => {
    console.error("Error fetching admin data: ", error);
  });
}

function handleAddData() {
  addDataDialogVisible.value = true; // 显示新增账号模态框
}

function handleAdd() {
  axios.post(`http://localhost:8080/user/addadmin`, addFormData.value)
    .then(() => {
      addDataDialogVisible.value = false;
      resetAddForm();
      getAdmin();
    })
    .catch((error) => {
      console.error("Error adding admin: ", error);
    });
}

function resetAddForm() {
  addFormData.value = {
    username: '',
    password: ''
  };
}

function handleEdit(row) {
  editFormData.value = { ...row };
  editDataDialogVisible.value = true;
}

function handleDelete(row) {
  axios.post(`http://localhost:8080/user/deleteadmin`, row)
    .then(() => {
      getAdmin();
    })
    .catch((error) => {
      console.error("Error deleting admin: ", error);
    });
}

function handleSave() {
  axios.put(`http://localhost:8080/user/updateadmin`, editFormData.value)
    .then(() => {
      editDataDialogVisible.value = false;
      getAdmin();
    })
    .catch((error) => {
      console.error("Error updating admin data: ", error);
    });
}

function resetEditForm() {
  editFormData.value = {
    username: '',
    password: ''
  };
}
</script>

<style scoped>
.account-management {
  padding: 20px;
}

.dialog-footer {
  text-align: right;
}
</style>